package supermarket;

import threads.*;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import model.Purchase;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class PurchaseXMLManager {

    private final DateFormat df;
    private File xmlFile;
    private boolean isNewDocument = true;

    public PurchaseXMLManager(File xmlFile) {
        this.xmlFile = xmlFile;
        df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    }

    private Document readXmlDocument() {
        Document document;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(xmlFile);
            isNewDocument = false;
            return document;
        } catch (Exception e) {
            // File does not exist
            isNewDocument = true;
            return null;
        }
    }

    public void writeXML(PurchaseThread thread) {
        Document document = readXmlDocument();
        try {
            if (isNewDocument) { // Create New file
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                document = docBuilder.newDocument();
                Element rootElement = document.createElement("purchases");
                document.appendChild(rootElement);
            }
            Element root = document.getDocumentElement();

            // purchase elements
            Element purchase = document.createElement("Purchase");
            root.appendChild(purchase);
            // thread name
            Element threadName = document.createElement("thread_name");
            threadName.appendChild(document.createTextNode(thread.getName()));
            purchase.appendChild(threadName);
            // retries
/*            Element retries = document.createElement("retries");
            for (int i = 0; i < thread.getRetriesTimestamps().size(); i++) {
                Calendar c = thread.getRetriesTimestamps().get(i);
                Element retry = document.createElement("retry_" + (i + 1));
                retry.appendChild(document.createTextNode(df.format(c.getTime())));
                retries.appendChild(retry);
            }
            purchase.appendChild(retries);
*/
        // purchase result
            Element purchaseResult = document.createElement("purchase_result");
            String presult;
            if (thread.isSubmitted()) {
                presult = "OK";
            } else {
                presult = "NOK";
            }
            purchaseResult.appendChild(document.createTextNode(presult));
            purchase.appendChild(purchaseResult);
            // invalid credit card
            if (!thread.isValidCreditCard()) {
                Element invalidccard = document.createElement("invalid_credit_card");
                invalidccard.appendChild(document.createTextNode("true"));
                purchase.appendChild(invalidccard);
            }

            if (thread.isSubmitted()) {


                Purchase p = thread.getThreadPurchase();

                // set attribute to purchase element
                Attr attr = document.createAttribute("id");
                attr.setValue(p.getPurchaseId().toString());
                purchase.setAttributeNode(attr);

                // date elements
                Element date = document.createElement("date");
                date.appendChild(document.createTextNode(p.getDatetime()));
                purchase.appendChild(date);

                // amount elements
                Element amount = document.createElement("amount");
                amount.appendChild(document.createTextNode(Float.toString(p.getAmount())));
                purchase.appendChild(amount);

                // points_earned element
                Element pointsEarned = document.createElement("points_earned");
                pointsEarned.appendChild(document.createTextNode(Integer.toString(p.getPointsEarned())));
                purchase.appendChild(pointsEarned);

                // delivery elements
                Element isCreditCardUsed = document.createElement("delivery");
                isCreditCardUsed.appendChild(document.createTextNode(Boolean.toString(p.getDelivery())));
                purchase.appendChild(isCreditCardUsed);

                // store elements
                Element store = document.createElement("store");
                store.appendChild(document.createTextNode(p.getStore().getName()));
                purchase.appendChild(store);

                // customer elements
                Element customer = document.createElement("customer");
                customer.appendChild(document.createTextNode(p.getCustomer().getLastName() + " " + p.getCustomer().getFirstName()));
                purchase.appendChild(customer);
            }

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(xmlFile);
            transformer.transform(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
